#include "stdafx.h"
#include "ui_single_object_control.h"

_SSL_BEGIN
_SSUIL_BEGIN

UISingleObjectControl::UISingleObjectControl()
{
	background_transparent_ = true;
}

UISingleObjectControl::~UISingleObjectControl()
{

}

UINT UISingleObjectControl::GetProperties()
{
	if(child_items_.size() == 0)
	{
		return __super::GetProperties();
	}

	return child_items_[0]->GetProperties();
}

void UISingleObjectControl::DoEvent(UIEvent &evt)
{
	if(child_items_.size() == 0)
	{
		__super::DoEvent(evt);
		return;
	}

	if(evt.type == kEvtMouseEnter)
	{
		if(to_over_control_ != overed_control_ && overed_control_ != NULL)
		{
			UIEvent evt2(evt);
			evt2.type = kEvtMouseLeave;
			overed_control_->DispatchEventOfControl(evt2);
			overed_control_ = NULL;
		}

		if(to_over_control_ != overed_control_ && to_over_control_ != NULL)
		{
			overed_control_ = to_over_control_;
			overed_control_->DispatchEventOfControl(evt);
		}
		child_items_[0]->DispatchEventOfControl(evt);
		return;
	}

	if(evt.type == kEvtMouseLeave)
	{
		if(overed_control_ != NULL)
		{
			overed_control_->DispatchEventOfControl(evt);
			overed_control_ = NULL;
		}
		child_items_[0]->DispatchEventOfControl(evt);
		return;
	}

	if(evt.type == kEvtMouseMove)
	{
		if(to_over_control_ != overed_control_ && overed_control_ != NULL)
		{
			UIEvent evt2(evt);
			evt2.type = kEvtMouseLeave;
			overed_control_->DispatchEventOfControl(evt2);
			overed_control_ = NULL;
		}

		if(to_over_control_ != overed_control_ && to_over_control_ != NULL)
		{
			overed_control_ = to_over_control_;

			UIEvent evt2(evt);
			evt2.type = kEvtMouseEnter;
			overed_control_->DispatchEventOfControl(evt2);
		}

		if(overed_control_ != NULL)
		{
			overed_control_->DispatchEventOfControl(evt);
		}

		if(pressed_control_ != NULL && !pressed_control_->compare(overed_control_.get()))
		{
			evt.type = kEvtMouseMove;
			pressed_control_->DispatchEventOfControl(evt);
		}

		child_items_[0]->DispatchEventOfControl(evt);

		return;
	}

	if(evt.type == kEvtLButtonDown)
	{
		if(to_press_control_ != pressed_control_ && pressed_control_ != NULL)
		{
			UIEvent evt2(evt);
			evt2.relative_control = NULL;
			evt2.type = kEvtButtonUp;
			evt2.wparam = WM_LBUTTONUP;
			pressed_control_->DispatchEventOfControl(evt2);
			pressed_control_ = NULL;
		}

		if(to_press_control_ != pressed_control_ && to_press_control_ != NULL)
		{
			pressed_control_ = to_press_control_;
			pressed_control_->DispatchEventOfControl(evt);
		}
		return;
	}

	if(evt.type == kEvtButtonUp && evt.wparam == WM_LBUTTONUP)
	{
		if(pressed_control_ != NULL)
		{
			evt.relative_control = to_press_control_;
			pressed_control_->DispatchEventOfControl(evt);
			pressed_control_ = NULL;
		}
		return;
	}

	child_items_[0]->DispatchEventOfControl(evt);
}

bool UISingleObjectControl::AddChild(IUIControlInterface *control)
{
	if(control == NULL)
		return false;
	if(child_items_.size() > 0)
		RemoveChild(child_items_[0]);
	SetPresetPosition(control->GetPresetPosition());
	control->SetPresetPosition(GetPresetPosition().SetLeft(0, true)
		.SetTop(0, true).SetRight(0, true).SetBottom(0, true));
	if(!__super::AddChild(control))
		return false;


	control->OnStateChanged += make_delegate(this, &UISingleObjectControl::DelegateOnItemStateChanged);
	control->OnEvent += make_delegate(this, &UISingleObjectControl::DelegateOnItemEvent);
	return true;
}

bool UISingleObjectControl::RemoveChild(IUIControlInterface *control)
{
	if(this->IsControlExists(control))
	{
		control->OnStateChanged -= make_delegate(this, &UISingleObjectControl::DelegateOnItemStateChanged);
		control->OnEvent -= make_delegate(this, &UISingleObjectControl::DelegateOnItemEvent);
	}
	if(!__super::RemoveChild(control))
		return false;
	return true;
}

scoped_ref_ptr<IUIControlInterface> UISingleObjectControl::FindControl(FindControlCB cb, void *data, unsigned int flags)
{
	if(SS_FLAG_ISSET(flags, kUIFindHittest))
	{
		if(child_items_.size() == 0)
			return NULL;

		UIFindControlByPoint *find_ctr_d = static_cast<UIFindControlByPoint*>(data);
		if(find_ctr_d == NULL)
			return NULL;

		if(find_ctr_d->for_over)
			to_over_control_ = NULL;
		if(find_ctr_d->for_press)
			to_press_control_ = NULL;

		scoped_ref_ptr<IUIControlInterface> ret = child_items_[0]->FindControl(cb, data, flags);
		if(ret == NULL)
			return NULL;

		if(find_ctr_d->for_press)
			to_press_control_ = ret;

		if(ret != child_items_[0])
		{
			if(find_ctr_d->for_over)
				to_over_control_ = ret;
		}

		return this;
	}
	return __super::FindControl(cb, data, flags);
}

void UISingleObjectControl::OnWindowManagerChangedCB(IUIManagerInterface *old_manager, IUIManagerInterface *new_manager)
{
	__super::OnWindowManagerChangedCB(old_manager, new_manager);
	if(new_manager != NULL)
	{
		if(child_items_.size() > 0)
		{
			child_items_[0]->OnStateChanged += make_delegate(this, &UISingleObjectControl::DelegateOnItemStateChanged);
			child_items_[0]->OnEvent += make_delegate(this, &UISingleObjectControl::DelegateOnItemEvent);
		}
	}else
	{
		if(child_items_.size() > 0)
		{
			child_items_[0]->OnStateChanged -= make_delegate(this, &UISingleObjectControl::DelegateOnItemStateChanged);
			child_items_[0]->OnEvent -= make_delegate(this, &UISingleObjectControl::DelegateOnItemEvent);
		}
	}
}

bool UISingleObjectControl::DelegateOnItemStateChanged(void *tag)
{
	if(child_items_.size() == 0)
		return true;
	if(OnStateChanged)
	{
		OnStateChanged((IUIControlInterface*)this);
	}
	return true;
}

bool UISingleObjectControl::DelegateOnItemEvent(void *tag)
{
	if(child_items_.size() == 0)
		return true;
	if(OnEvent)
	{
		OnEvent(tag);
	}
	return true;
}

_SSUIL_END
_SSL_END
